package com.lxmes.sales.service.impl;

import java.util.Date;
import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.lxmes.base.domain.NcSupplier;
import com.lxmes.base.mapper.NcSupplierMapper;
import com.lxmes.common.utils.SecurityUtils;
import com.lxmes.common.utils.bean.BeanUtils;
import com.lxmes.sales.domain.ZcSupplierConversion;
import com.lxmes.sales.mapper.ZcSupplierConversionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lxmes.common.utils.StringUtils;
import com.lxmes.common.utils.bean.BeanValidators;
import com.lxmes.common.exception.ServiceException;
import javax.validation.Validator;
import com.lxmes.sales.mapper.ZcSupplierRegistrationMapper;
import com.lxmes.sales.domain.ZcSupplierRegistration;
import com.lxmes.sales.service.IZcSupplierRegistrationService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 供方注册及审核Service业务层处理
 *
 * @author lx
 * @date 2023-06-21
 */
@Service
public class ZcSupplierRegistrationServiceImpl implements IZcSupplierRegistrationService
{
    @Autowired
    private ZcSupplierRegistrationMapper zcSupplierRegistrationMapper;


    @Autowired
    private NcSupplierMapper ncSupplierMapper;

    @Autowired
    private ZcSupplierConversionMapper zcSupplierConversionMapper;

    @Autowired
    protected Validator validator;
    /**
     * 查询供方注册及审核
     *
     * @param id 供方注册及审核主键
     * @return 供方注册及审核
     */
    @Override
    public ZcSupplierRegistration selectZcSupplierRegistrationById(Long id)
    {
        return zcSupplierRegistrationMapper.selectZcSupplierRegistrationById(id);
    }

    /**
     * 查询供方注册及审核列表
     *
     * @param zcSupplierRegistration 供方注册及审核
     * @return 供方注册及审核
     */
    @Override
    public List<ZcSupplierRegistration> selectZcSupplierRegistrationList(ZcSupplierRegistration zcSupplierRegistration)
    {
        return zcSupplierRegistrationMapper.selectZcSupplierRegistrationList(zcSupplierRegistration);
    }

    /**
     * 新增供方注册及审核
     *
     * @param zcSupplierRegistration 供方注册及审核
     * @return 结果
     */
    @Override
    public int insertZcSupplierRegistration(ZcSupplierRegistration zcSupplierRegistration)
    {
        zcSupplierRegistration.setRegistrationTime(new Date());
        zcSupplierRegistration.setRegistrationDate(new Date());
        zcSupplierRegistration.setRegistrant(SecurityUtils.getUsername());
        zcSupplierRegistration.setCreateBy(SecurityUtils.getUsername());
        zcSupplierRegistration.setCreateTime(new Date());
        zcSupplierRegistration.setDeptId(SecurityUtils.getDeptId());
        return zcSupplierRegistrationMapper.insertZcSupplierRegistration(zcSupplierRegistration);
    }

    /**
     * 修改供方注册及审核
     *
     * @param zcSupplierRegistration 供方注册及审核
     * @return 结果
     */
    @Override
    public int updateZcSupplierRegistration(ZcSupplierRegistration zcSupplierRegistration)
    {
        return zcSupplierRegistrationMapper.updateZcSupplierRegistration(zcSupplierRegistration);
    }

    /**
     * 批量删除供方注册及审核
     *
     * @param ids 需要删除的供方注册及审核主键
     * @return 结果
     */
    @Override
    public int deleteZcSupplierRegistrationByIds(Long[] ids)
    {
        return zcSupplierRegistrationMapper.deleteZcSupplierRegistrationByIds(ids);
    }

    /**
     * 删除供方注册及审核信息
     *
     * @param id 供方注册及审核主键
     * @return 结果
     */
    @Override
    public int deleteZcSupplierRegistrationById(Long id)
    {
        return zcSupplierRegistrationMapper.deleteZcSupplierRegistrationById(id);
    }
    @Override
    public String imporIteminfo(List<ZcSupplierRegistration> zcSupplierRegistrationList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(zcSupplierRegistrationList) || zcSupplierRegistrationList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (ZcSupplierRegistration zcSupplierRegistration : zcSupplierRegistrationList)
        {
            try
            {
                BeanValidators.validateWithException(validator, zcSupplierRegistration);
                zcSupplierRegistration.setCreateBy(operName);
                zcSupplierRegistration.setUpdateBy(operName);
                this.insertZcSupplierRegistration(zcSupplierRegistration);
                successNum++;
                successMsg.append("<br/>" + successNum + "数据导入成功");
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/> 数据导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    public int auditZcSupplierRegistration(ZcSupplierRegistration zcSupplierRegistration) {
        ZcSupplierRegistration zcSupplierRegistration1 = this.selectZcSupplierRegistrationById(zcSupplierRegistration.getId());
        if ("0".equals(zcSupplierRegistration1.getAuditState())) {
            zcSupplierRegistration1.setAuditTime(new Date());
            zcSupplierRegistration1.setAuditor(SecurityUtils.getUsername());
            zcSupplierRegistration1.setAuditState(zcSupplierRegistration.getAuditState());
        } else {
            throw new ServiceException("供方信息已审核");
        }
        return zcSupplierRegistrationMapper.updateZcSupplierRegistration(zcSupplierRegistration1);
    }

    @Override
    @Transactional
    public int toSupplier(Long id) {
        ZcSupplierRegistration zcSupplierRegistration = selectZcSupplierRegistrationById(id);
        if("1".equals(zcSupplierRegistration.getIsSupplier())){
            throw new RuntimeException("已转为供应商，不能再转！");
        }
        NcSupplier ncSupplier = new NcSupplier();
        ncSupplier.setSupplierName(zcSupplierRegistration.getSupplierName());
        ncSupplier.setSupplierCode(zcSupplierRegistration.getSupplierCode());
        ncSupplier.setAddress(zcSupplierRegistration.getAddress());
        ncSupplier.setSupplierPhone(zcSupplierRegistration.getSupplierPhone());
        ncSupplierMapper.insertNcSupplier(ncSupplier);
        ZcSupplierConversion zcSupplierConversion = new ZcSupplierConversion();
        BeanUtils.copyProperties(zcSupplierRegistration,zcSupplierConversion);
        zcSupplierConversionMapper.insertZcSupplierConversion(zcSupplierConversion);
        zcSupplierRegistration.setIsSupplier("1");
       return zcSupplierRegistrationMapper.updateZcSupplierRegistration(zcSupplierRegistration);
    }
}
